//
//  YDLogInView.swift
//  weibo
//
//  Created by 孙亚东 on 16/6/23.
//  Copyright © 2016年 Sunyadong. All rights reserved.
//

import UIKit


protocol YDLogInViewDelegate: NSObjectProtocol{

    func loginMethod()

    func registerMethod()
    
    
}

class YDLogInView: UIView {
    
    //定义一个代理属性
    weak var delegate: YDLogInViewDelegate?

    //重写init方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        addSubview(image1)
        addSubview(coverView)
        addSubview(imageHouse)
        addSubview(lab1)
        addSubview(BtnLogin)
        addSubview(BtnRegister)
        
        setupUI()
       
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //设置布局
    func setupUI () {
    
        image1.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
    
        }
        
        
        //设置蒙版的布局
        coverView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
            
            
            
        }
        
        imageHouse.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.snp_center)
            
    
        }
        
        
        lab1.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(image1.snp_bottom).offset(10)
            
            make.width.equalTo(210)
            
        
        }

        BtnLogin.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(lab1.snp_left)
            make.top.equalTo(lab1.snp_bottom).offset(10)
            
            
            make.width.equalTo(100)
            make.height.equalTo(30)
            
            
            
            
        }
        
        BtnRegister.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(BtnLogin)
            make.right.equalTo(lab1.snp_right)
            

            make.width.equalTo(100)
            make.height.equalTo(30)
            
            
            
        }
        
        addAnimation()
        

    }
    
    //添加动画
    
    func addAnimation() {
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.duration = 10.0
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        image1.layer.addAnimation(anim, forKey: nil)
        

        
    }
    
    func setupInfo(title: String, img: String?) {
        
        
        //如果img为空
        if let imag = img {
        
            //证明是首页
            
            image1.hidden = true
            imageHouse.image = UIImage(named: imag)
            
        
        }else{
        
            
            image1.hidden = false
            
            image1.startAnimating()
        
        
        }
        
        lab1.text = title
        
    
    
    
    }
    
    
    
    
    
    //懒加载控件
    lazy var image1: UIImageView = {
    
        var image1 = UIImageView()
        
        image1.image = UIImage(named: "visitordiscover_feed_image_smallicon")

        return image1
    
    }()
    
    
    lazy var imageHouse: UIImageView = {
        
        var image2 = UIImageView()
        
        image2.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return image2

    }()
    
    
    lazy var lab1: UILabel = {
    
        var lab1 = UILabel()
        
        lab1.text = "你大爷永远是你大爷,你大爷永远是你大夜的说法是电风扇"
        
        lab1.textAlignment = .Center
        
        lab1.numberOfLines = 5
        
        lab1.font = UIFont.systemFontOfSize(12.0)
        
        return lab1

    
    }()
    
    
    lazy var BtnLogin: UIButton = {
    
        var btn = UIButton(type: UIButtonType.Custom)
        
        btn.addTarget(self, action: "logInBtnClick:", forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        return btn
    
    
    }()
    
    lazy var BtnRegister: UIButton = {
        
        var btn = UIButton(type: UIButtonType.Custom)
        
        btn.addTarget(self, action: "ClickRegisterBtn:", forControlEvents: .TouchUpInside)
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        
        return btn
        
        
    }()
    
    
    //插入蒙版
    lazy var coverView: UIImageView = {
    
    
        var coverView = UIImageView()
        
        
        coverView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        
        return coverView
    
    
    
    }()
    
    
    func logInBtnClick (btn: UIButton){
    
    
        //通过代理实现
        delegate?.loginMethod()
    
        
    }
    
    func ClickRegisterBtn (btn: UIButton){
    
    
        delegate?.registerMethod()
    
    
    
    }
    

}
